#include"ConcurrentAlloc.h"

void Aloc1()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}
void Aloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(7);
	}
}
void TLSTset()
{
	std::thread t1(Aloc1);
	t1.join();

	std::thread t2(Aloc2);

	t2.join();
}

void TestConcurrentAlloc2()
{
	for (int i = 0; i < 1024; i++)
	{
		void* p2 = ConcurrentAlloc(6);
		cout << p2 << endl;
	}
	void* p3 = ConcurrentAlloc(8);
	cout << p3 << endl;
	

}
void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);
	void* p6 = ConcurrentAlloc(8);
	void* p7 = ConcurrentAlloc(8);



	ConcurrentFree(p1, 6);
	ConcurrentFree(p2, 8);
	ConcurrentFree(p3, 1);
	ConcurrentFree(p4, 7);
	ConcurrentFree(p5, 8);
	ConcurrentFree(p6, 8);
	ConcurrentFree(p7, 8);






}
void TestAddressShift()
{
	PAGE_ID id1 = 2000;
	PAGE_ID id2 = 2001;
	char* p1 = (char*)(id1 << PAGE_SHIFT);
	char* p2 = (char*)(id2 << PAGE_SHIFT);
	cout << p1 << endl;
	cout << p2 << endl;

	while (p1 < p2)
	{
		cout << (void*)p1 << ((PAGE_ID)p1 >> PAGE_SHIFT) << endl;
		p1 += 8;
	}
}
void TestMutilThtead1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 1024; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (auto e : v)
	{
		ConcurrentFree(e, 6);
	}
}
void TestMutilThtead2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 1024; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (auto e : v)
	{
		ConcurrentFree(e, 6);
	}
}
void TestMutilThtead()
{
	std::thread t1(TestMutilThtead1);
	t1.join();

	std::thread t2(TestMutilThtead2);

	t2.join();
}
int main()
{
	TestMutilThtead();
	//TestConcurrentAlloc1();
	//TestAddressShift();

	return 0;
}